package cn.yuanqiao.common.utils;

import com.alibaba.fastjson2.JSONArray;
import com.lowagie.text.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zjj
 * @date 2023/9/11
 */
public class PdfUtils {
    protected static final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
    public static void main(String[] args) throws IOException{
        //File a = new File("C:\\Users\\Lenovo\\Desktop\\22732718107486208001A001_0.jpg");
        //System.out.printf(a.getParent()+File.separator);
        //    List<String> a =pdf2Img("C:\\Users\\Lenovo\\Desktop\\导入测试文件\\A0000006971001.pdf","");
        //  System.out.printf(JSONArray.from(a).toString());
        // int count=getPageCount("D:\\yq\\infoManage_1710294363076(3).PDF");
        //   System.out.println(count);
    }
    static volatile int num = 0;
    /**
     * pdf转图片
     * 转换全部的pdf
     * @param fileAddress PDF文件绝对路径
     * @return ArrayList<String>    生成imgs文件群的绝对路径集合
     */
    public static List<String> pdf2Img(String fileAddress,String outAddress) throws IOException{
        ArrayList<String> fileAddresss = new ArrayList<>();
        PDDocument doc = null;
        logger.info("正在执行的任务个数="+num);
        try {
            while (num > 1){ //每次只能有2个任务同时进行
                Thread.sleep(1000);
            }
            num ++;
            // 将pdf装图片 并且自定义图片得格式大小
            File file = new File(fileAddress);
            doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                //经过测试,dpi为96,100,105,120,150,200中,105显示效果较为清晰,体积稳定,dpi越高图片体积越大,一般电脑显示分辨率为96
                BufferedImage image = renderer.renderImageWithDPI(i, 105); // Windows native DPI
                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
                //String substring = fileAddress.substring(0, fileAddress.lastIndexOf("."));
                String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
                ImageIO.write(image, "jpg", new File(outAddress+fileName + "_" + i + ".jpg"));
                fileAddresss.add(outAddress+fileName + "_" + i + ".jpg");
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }finally {
            num--;
            if (doc != null) {
                doc.close();
            }
        }
        return fileAddresss;
    }

    public static int PdfPage(String fileAddress){
        int pageCount = 0;
        try {
            File file = new File(fileAddress);
            PDDocument doc = PDDocument.load(file);
            pageCount = doc.getNumberOfPages();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return pageCount;
    }
    public static int getPageCount(String filePath) throws IOException {
        try (PDDocument document = PDDocument.load(new File(filePath))) {
            return document.getNumberOfPages();
        }
    }
    public static String getPageLine(String filePath) throws IOException {
        //加载pdf文档
        File file=new File(filePath);
        if(file.exists()) {
            PDDocument document = PDDocument.load(file);
            //创建PDFTextStripper
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setStartPage(1);
            stripper.setEndPage(document.getNumberOfPages());
            //提取PDF文档内容为字符串
            String text = stripper.getText(document);
            String[] lines = text.split("\\r?\\n");
            document.close();
            return lines[0];
        }else {
            return "";
        }
    }


    /**
     * 自由确定起始页和终止页
     *
     * @param fileAddress  文件绝对路径
     * @param indexOfStart 开始页  开始转换的页码，从0开始
     * @param indexOfEnd   结束页  停止转换的页码，
     */
    public static void pdf3Img(String fileAddress, int indexOfStart, int indexOfEnd) throws IOException {
        // 将pdf装图片 并且自定义图片得格式大小
        File file = new File(fileAddress);
        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = indexOfStart; i < indexOfEnd; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 105); // Windows native DPI
                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
                String substring = fileAddress.substring(0, fileAddress.lastIndexOf("."));
                ImageIO.write(image, "jpg", new File(substring + "_" + (i + 1) + ".jpg"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (doc != null) {
                doc.close();
            }
        }
    }

    private static BufferedImage resize(BufferedImage source, int targetW,  int targetH) {
        int type=source.getType();
        BufferedImage target=null;
        double sx=(double)targetW/source.getWidth();
        double sy=(double)targetH/source.getHeight();
        if(sx>sy){
            sx=sy;
            targetW=(int)(sx*source.getWidth());
        }else{
            sy=sx;
            targetH=(int)(sy*source.getHeight());
        }
        if(type==BufferedImage.TYPE_CUSTOM){
            ColorModel cm=source.getColorModel();
            WritableRaster raster=cm.createCompatibleWritableRaster(targetW, targetH);
            boolean alphaPremultiplied=cm.isAlphaPremultiplied();
            target=new BufferedImage(cm,raster,alphaPremultiplied,null);
        }else{
            target=new BufferedImage(targetW, targetH,type);
        }
        Graphics2D g=target.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }
}
